package com.city.itis.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.city.itis.dao.GuestDao;
import com.city.itis.domain.Guest;
import com.city.itis.domain.Member;


/**
 * 会员Dao实现类
 * @author WY
 *
 */
@Component("guestDao")
public class GuestDaoImpl implements GuestDao {
	
	private HibernateTemplate hibernateTemplate;
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


	@Override
	public int save(Guest guest) 
	{
		
		try
		{
			int id=(Integer)this.hibernateTemplate.save(guest);
			return id;
			
		}catch(Exception e)
		{
			//throw new AppException("系统出现错误，请联系管理员。");
			return 0;
		}
	}

	@Override
	public int delete(Guest guest) {
		try {
			this.hibernateTemplate.delete(guest);
		} catch (Exception e) {
		return 0;
		}
		return 1;
	}

	@Override
	public int deleteById(Integer id) {
		Guest guest=(Guest) this.getHibernateTemplate().get(Guest.class, id);
		if(guest!=null){
		try {
			this.hibernateTemplate.delete(guest);
		} catch (Exception e) {
			return 0;
		}
		return 1;
		}
		return 0;
	}

	@Override
	public int update(Guest guest) {
		try {
			this.hibernateTemplate.update(guest);
		} catch (Exception e) {
			return 0;
		}
		return 1;
	}

	@Override
	public Guest getGuestById(Integer id) {
		Guest guest=(Guest)this.getHibernateTemplate().get(Guest.class, id);
	     if(guest!=null){
	    	 return guest;
	     }
		return null;
	}

	@Override
	public List<Guest> findAll() {
		String queryString="FROM Guest ";
		@SuppressWarnings("unchecked")
		List<Guest> guestList=(List<Guest>)this.hibernateTemplate.find(queryString);
		if(guestList!=null){
			return guestList;
		}
		return null;
	}
	
	@Override
	public int login(Integer id,String password)
	{
		Guest guest=(Guest)this.getHibernateTemplate().get(Guest.class, id);
	     if(guest!=null){
	    	 if(guest.getPassword().equals(password))
	    	 {
	    		 return 1;
	    	 }
	     }
		return 0;
	}

}
